package com.itheima.mm.controller;

import com.itheima.mm.dto.SimpleDto;
import com.itheima.mm.pojo.PageResult;
import com.itheima.mm.pojo.Result;
import com.itheima.mm.pojo.User;
import com.itheima.mm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * heimamm
 *
 * @ClassName UserController
 * @Description
 * @Author 吴志强 --游侠波克比
 * @Date 2022/1/9 21:04
 * @Version V1.0
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 根据id查找user(用户)
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public User findById(Integer id){
        User user = userService.getById(id);
        return user;
    }

    /**
     * id查用户
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public User findId(@PathVariable Integer id){
        User user = userService.getById(id);
        return user;
    }

    /**
     * 根据id修改user
     * @param id
     * @param user
     * @return
     */
    @PutMapping("/{id}")
    public String update(@PathVariable Integer id, @RequestBody User user){
        //将请求的id,封装
        user.setId(id);
        userService.save(user);

        return "更改成功";
    }

    /**
     * 条件分页查询
     * @param page
     * @param pagesize
     * @param username
     * @return
     */
    @GetMapping({"/findPage",""})
    public PageResult findPage(Integer page,Integer pagesize,String username){
        PageResult userPage = userService.findPage(page, pagesize, username);
        return userPage;
    }

    /**
     * 由id删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public String deleteById(@PathVariable Integer id){
        userService.removeById(id);
        return "删除成功";
    }

    /**
     * 修改用户的状态,是否启用
     * @return
     */
    @PutMapping("/{id}/disabled")
    public String disabledStatus(@PathVariable Integer id,Integer disabled){
        userService.disabledStatus(id,disabled);
        return "状态更改成功";
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping
    public String saveUser(@RequestBody User user){
        userService.save(user);
        return "新增成功";
    }

    /**
     * 简单查询
     * @param keyword
     * @param disabled
     * @return
     */
    @GetMapping("/simple")
    public List<SimpleDto> simple(String keyword, Integer disabled){
        List<SimpleDto> simples = userService.simple(keyword, disabled);
        return simples;
    }

    /**
     * 更改密码
     * @param id
     * @param password
     * @return
     */
    @PutMapping("/{id}/password")
    public String updatePass(@PathVariable Integer id,String password){
        //更改用户米买
        Boolean pass = userService.updataPass(id, password);
        if(!pass){
            return "用户不存在";
        }

        return "密码更改成功";
    }


}
